package com.springBoot.springBootSysCore.modules.controller.web.system;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.springBoot.springBootSysCore.common.persistence.DataTables;
import com.springBoot.springBootSysCore.common.utils.FileUtil;
import com.springBoot.springBootSysCore.common.utils.returnUtil.AjaxJson;
import com.springBoot.springBootSysCore.common.utils.returnUtil.PageUtils;
import com.springBoot.springBootSysCore.modules.controller.web.base.BaseController;
import com.springBoot.springBootSysCore.modules.entity.system.SysIntro;
import com.springBoot.springBootSysCore.modules.entity.system.SysResource;
import com.springBoot.springBootSysCore.modules.services.system.SysIntroService;
import com.springBoot.springBootSysCore.modules.services.system.SysResourceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.lang.model.element.Name;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Map;

/**
 */
@Controller
@RequestMapping("${adminPath}/sysResource")
public class SysResourceController extends BaseController<SysResource,String> {
    @Autowired
    private SysResourceService sysResourceService;

    @ModelAttribute("entity")
    public SysResource get(@RequestParam(required = false) String id) {
        if (StringUtils.isNotBlank(id)) {
            return sysResourceService.findById(id);
        } else {
            return new SysResource();
        }
    }
    /*TreeGrid*/
    @RequestMapping(value = "treeData")
    @ResponseBody
    public Object treeData(@RequestParam(required = false)String pid){
        return sysResourceService.findTreeResOfRes(pid);
    }
    /*ztree*/
    @RequestMapping(value = "ztreeDataNoUrl")
    @ResponseBody
    public Object ztreeDataNoUrl(@RequestParam(name="extId",required = false)String extId){
        return sysResourceService.ztreeDataNoUrl(extId);
    }
    /*layui菜单*/
    @RequestMapping(value = "layuiMenu")
    @ResponseBody
    public Object layuiMenu(){
        return sysResourceService.layuiMenu();
    }

    @RequestMapping(value="findOne",method= RequestMethod.GET)
    public String findOne(String id,String detail,Model model){
        model.addAttribute("detail",detail);
        return "system/resource/resourceChange";
    }
    /**
     * 保存
     * @param entity
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping(value="saveEntity",method=RequestMethod.POST)
    @ResponseBody
    public Object saveEntity(SysResource entity, Model model, HttpServletResponse response,HttpServletRequest request) throws JsonProcessingException{
        if (!beanValidator(model, entity)){
            return renderString(response, new AjaxJson().setMsg(model.asMap().get("message").toString()).setSuccess(false));
        }
        return sysResourceService.saveResource(entity);
    }
    @RequestMapping(value="deleteEntity",method=RequestMethod.POST)
    @ResponseBody
    public Object deleteEntity(SysResource entity) throws JsonProcessingException{
        return sysResourceService.deleteResource(Arrays.asList(entity.getId()));
    }

}
